package com.cskaoyan.service;



import com.cskaoyan.mapper.marketMapper.MarketAdminMapper;
import com.cskaoyan.bean.marketBean.MarketAdmin;
import com.cskaoyan.bean.marketBean.MarketAdminExample;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author stone
 * @date 2022/09/06 11:44
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService{

    @Autowired
    MarketAdminMapper adminMapper;


    @Override
    public MarketAdmin queryById(Integer id) {
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        return marketAdmin;
    }

    @Override
    public void modify(MarketAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public List<MarketAdmin> query(String username, String password, String[] avatars,
                                   Integer page, Integer limit) {
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        example.setDistinct(true);
        //example.setOrderByClause("username asc");
        // List<Criterion> 向这个列表里塞入数据
        // 条件查询

        MarketAdminExample.Criteria criteria = example.createCriteria();//这个实例里封装了Criterion的列表
        // 使用Example中的Criteria提供的方法，就可以构造Criterion实例，并且呢添加到list中
        /*criteria.andUsernameEqualTo(username)
                .andPasswordLike("%" + password + "%")
                .andAvatarIn(Arrays.asList(avatars));*/
        if (username != null && !"".equals(username)) {//StringUtils isEmpty
            criteria.andUsernameEqualTo(username);
        }
        if (password != null && !"".equals(password)) {

            criteria.andPasswordLike("%" + password + "%");
        }
        if (avatars != null && avatars.length > 0) {

            criteria.andAvatarIn(Arrays.asList(avatars));
        }
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        // page limit total
        // 把mybatis查询出来的原始结果放进去
        PageInfo pageInfo = new PageInfo(marketAdmins);
        long total = pageInfo.getTotal();
        return marketAdmins;
    }


}
